<template>
  <div class="container">
    <!-- 预览文章对话框 -->
    <el-dialog
      title="新增文章"
      :visible="showAddDialog"
      width="60%"
      @close="handleClose"
      @open="handleOpen"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80px"
        size="small"
      >
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor
            v-model="form.articleBody"
            :options="editorOption"
            @blur="blurQuillEditor"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop="videoURL">
          <el-input
            v-model="form.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleClose" size="small">取 消</el-button>
          <el-button type="primary" @click="submitAdd" size="small"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/articles.js'
// 导入富文本编辑器
import { quillEditor } from 'vue-quill-editor'
// 导入富文本编辑器样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
const toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
  [{ list: 'ordered' }, { list: 'bullet' }], // 列表
  ['blockquote', 'code-block'], // 引用，代码块
  ['image', 'link'] // 上传图片、上传链接
]
export default {
  components: { quillEditor },
  props: {
    showAddDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      // 校验规则
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容' }
        ]
      },
      //
      editorOption: { //  富文本编辑器配置
        modules: {
          toolbar: {
            container: toolbarOptions // 工具栏
          }
        },
        placeholder: ''
      }
    }
  },

  methods: {
    // 关闭对话框
    handleClose () {
      this.$emit('update:showAddDialog', false)
      this.$refs.formRef.resetFields()
    },
    // 打开对话框
    handleOpen () {
      this.$emit('update:showAddDialog', true)
    },
    // 校验富文本
    blurQuillEditor () {
      this.$refs.formRef.validateField('articleBody')
    },

    // 提交添加表单
    submitAdd () {
      this.$refs.formRef.validate(async flag => {
        if (!flag) return
        await add(this.form)
        this.$message.success('新增成功')
        // 关闭对话框
        this.handleClose()
        // 重新渲染文章列表
        this.$emit('add-articles')
      })
    }

  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-button--default {
  margin-left: 550px !important;
}
::v-deep .ql-editor {
  height: 200px;
}
</style>
